package com.totcy.atom.model;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.totcy.atom.CommonValues;
import com.totcy.atom.bean.BannerBean;
import com.totcy.atom.net.HttpManager;
import com.totcy.atom.net.exception.ApiException;
import com.totcy.atom.net.subscriber.BaseHttpSubscriber;
import com.totcy.atom.presenter.contract.BannerContract;
import com.totcy.baselibrary.base.mvpbase.BaseModelImpl;
import com.totcy.baselibrary.httpcache.ACache;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * description : BannerModel
 *
 * @author : Totcy
 * @date : 2017/12/8 18:56
 * @update : 2017/12/8
 */

public class BannerModel extends BaseModelImpl implements BannerContract.Model {

    private BaseHttpSubscriber<List<BannerBean>> subscriber;
    private ACache aCache;
    private Gson gson = new Gson();

    public BannerModel(Context context) {
        super(context);
        aCache = ACache.get(context);
    }

    @Override
    public void getBannerList(final BannerContract.Model.DataCallback callback) {
        //gank数据并不是这个项目的主要服务器，因此没有使用同一的封装

        HttpManager.getApiService(mContext)
                .getBannerList(CommonValues.DAILY_SENTENCE_KYMJS)
                //线程调度
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //结果处理
                .subscribe(subscriber = new BaseHttpSubscriber<List<BannerBean>>() {
                    @Override
                    public void onSuccess(List<BannerBean> banners) {
                        callback.onBannerList(banners);
                        aCache.put(CommonValues.DAILY_SENTENCE_KYMJS, gson.toJson(banners));
                    }

                    @Override
                    public void onError(int code, String errorMsg) {
                        if (code == ApiException.Code_UnConnected) {
                            String json = aCache.getAsString(CommonValues.DAILY_SENTENCE_KYMJS);
                            if (!TextUtils.isEmpty(json)) {
                                List<BannerBean> banners = gson.fromJson(
                                        json,
                                        new TypeToken<List<BannerBean>>() {
                                        }.getType()
                                );
                                callback.onBannerList(banners);
                            }

                        }
                    }
                });

        //测试 解析码云上文件
       /* Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {

                try {
                    Document document = Jsoup.connect("https://gitee.com/totcy/Atom/blob/master/screenshot/apitest").get();
                    Element imageListelement = document.getElementById("blob_raw");
                    e.onNext(imageListelement.val());

                    Document document2 = Jsoup.connect("https://gitee.com/totcy/Atom/blob/master/screenshot/apixml").get();
                    Element imageListelement2 = document2.getElementById("blob_raw");
                    e.onNext(imageListelement2.val());

                } catch (IOException exception) {
                    e.onError(exception);
                }
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        Log.e("Tag", s);
                    }
                });*/
    }

    @Override
    public void canCelRequest() {
        if (subscriber != null) {
            subscriber.unSubscribe();
        }
    }
}
